<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: jsonUp.class.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
defined('IN_TWCMS') || exit('Access Denied');

class jsonUp extends admin{
	function __construct() {
		error_reporting(E_ERROR|E_WARNING);
		parent::__construct();
		if(isset($_GET['do']) && in_array($_GET['do'], array('up_image', 'up_scrawl', 'up_file', 'get_remote', 'get_manager', 'get_movie'))) {
			$this->$_GET['do']();
		}
		exit();
	}

	#上传图片
	function up_image() {
		$config = array('maxSize'=>$this->G['up_img_max_size'], 'allowExt'=>$this->G['up_img_ext']);
		$up = new upload('upfile', $config);
		$info = $up->getFileInfo();
		$this->insert_attachment($info);
		$title = empty($_POST['pictitle']) ? '' : htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
		echo '{"url":"'.TWCMS_UPLOAD.$info['url'].'","title":"'.$title.'","original":"'.$info['name'].'","state":"'.$info['state'].'"}';
	}

	#上传涂鸦
	function up_scrawl() {
		$config = array('maxSize'=>$this->G['up_img_max_size'], 'allowExt'=>$this->G['up_img_ext'], 'tmpDir'=>'tmpimage');

		if(isset($_GET['action']) && $_GET['action']=='tmpImg') {
			$up = new upload('upfile', $config);
			$info = $up->getFileInfo();
			echo '<script>parent.ue_callback("'.TWCMS_UPLOAD.$info['url'].'","'.$info['state'].'")</script>';
		}else{
			$tmpDir = TWCMS_ROOT.TWCMS_UPLOAD.$config['tmpDir'];
			if(file_exists($tmpDir)) delDir($tmpDir);

			$up = new upload('content', $config, true);
			$info = $up->getFileInfo();
			$this->insert_attachment($info);
			echo '{"url":"'.TWCMS_UPLOAD.$info['url'].'",state:"'.$info['state'].'"}';
		}
	}

	#上传文件
	function up_file() {
		$config = array('maxSize'=>$this->G['up_file_max_size'], 'allowExt'=>$this->G['up_file_ext']);
		$up = new upload('upfile', $config);
		$info = $up->getFileInfo();
		$this->insert_attachment($info);
		echo $str='{"url":"'.TWCMS_UPLOAD.$info["url"].'","fileType":"'.$info["type"].'","original":"'.$info["name"].'","state":"'.$info["state"].'"}';
	}

	#获取远程文件
	function get_remote() {
	}

	#获取图片管理
	function get_manager() {
		if(empty($_POST['action']) || $_POST['action']!='get') return;
		$query = $this->db->query('SELECT filepath FROM `'.TW_DBPRE.'attachment` WHERE uid='.$this->auser['uid'].' AND isimage=1 ORDER BY `aid` DESC LIMIT 32');
		$str = '';
		while($row = $this->db->fetch_array($query)) {
			$str.= TWCMS_UPLOAD.$row['filepath'].'ue_separate_ue';
		}
		echo $str;
	}

	#获取视频
	function get_movie() {
		if(empty($_POST['searchKey']) || empty($_POST['videoType'])) return;
		$key = htmlspecialchars($_POST['searchKey']);
		$type = htmlspecialchars($_POST['videoType']);
		$html = file_get_contents("http://api.tudou.com/v3/gw?method=item.search&appKey=myKey&format=json&kw=".$key."&pageNo=1&pageSize=20&channelId=".$type."&inDays=7&media=v&sort=s");
		echo $html;
	}

	#写入数据库
	function insert_attachment($info) {
		$Arr = array();
		if(empty($info['state']) || $info['state']!='SUCCESS') return false; 
		$Arr['filename'] = $info['name'];
		$Arr['filetype'] = $info['type'];
		$Arr['filesize'] = $info['size'];
		$Arr['filepath'] = $info['url'];
		$Arr['uid'] = $this->auser['uid'];
		$Arr['time'] = time();
		$Arr['ip'] = ip();
		$Arr['isimage'] = $info['isimage'];
		return $this->db->insert('attachment', $Arr);
	}
}